package com.jianyun.jyzs.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.Switch;
import android.widget.TextView;

import com.google.gson.Gson;
import com.hannesdorfmann.mosby3.mvp.MvpActivity;
import com.jianyun.jyzs.MainActivity;
import com.jianyun.jyzs.R;
import com.jianyun.jyzs.activity.newsign.NewSignReCordActivity;
import com.jianyun.jyzs.http.Api;
import com.jianyun.jyzs.http.RetrofitHelper;
import com.jianyun.jyzs.presenter.FlowSwtichPresenter;
import com.jianyun.jyzs.utils.LoginCache;
import com.jianyun.jyzs.utils.UtilsStyle;
import com.jianyun.jyzs.view.iview.IFlowSwtichView;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * @author 杨仕俊
 * @description 流程审批分类开关界面
 * <p>
 * Created by yangshijun on 2018/11/14.
 */

public class FlowSwtichActivity extends MvpActivity<IFlowSwtichView, FlowSwtichPresenter> implements IFlowSwtichView {


    @BindView(R.id.topBack)
    ImageView topBack;
    @BindView(R.id.topText)
    TextView topText;
    @BindView(R.id.switch_btn)
    Switch switchBtn;
    private String userId;
    private String enterpriseCode;
    private int isEnable;
    private boolean isSwitch = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        UtilsStyle.setStatusBarMode(this, true);
        setContentView(R.layout.activity_flow_switch);
        ButterKnife.bind(this);
        userId = LoginCache.getInstance().getUserInfo().getUserId();
        enterpriseCode = LoginCache.getInstance().getLoginCache().getEnterpriseCode();
        initView();
    }

    private void initView() {
        isSwitch = false;
        topBack.setVisibility(View.VISIBLE);
        topText.setText("审批设置");
        int flowSwtich = LoginCache.getInstance().getFlowSwtich();
        if (2 == flowSwtich) {
            switchBtn.setChecked(true);
        }
        switchBtn.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    saveSwtich(1);
                } else {
                    saveSwtich(0);
                }
                isSwitch = true;
            }
        });
    }

    public void saveSwtich(int swtich) {
        Api api = RetrofitHelper.builderErpRoot().create(Api.class);
        api.getFlowSwtich("SaveUserFlowSwitch", userId, swtich + "")
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new Subscriber<Object>() {
                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onNext(Object o) {
                        Log.i("test", "保存之后：" + o);
                        try {
                            Gson gson = new Gson();
                            String toJson = gson.toJson(o);
                            org.json.JSONObject jsonObject = new org.json.JSONObject(toJson);
                            boolean result = jsonObject.getBoolean("result");
                            if (result) {
                                isEnable = jsonObject.getInt("isEnable");
                                if (1 == isEnable) {
                                    LoginCache.getInstance().setFlowSwtich(2);
                                } else {
                                    LoginCache.getInstance().setFlowSwtich(1);
                                }

                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }

                    }
                });
    }

    @NonNull
    @Override
    public FlowSwtichPresenter createPresenter() {
        return new FlowSwtichPresenter();
    }

    @OnClick(R.id.topBack)
    public void onViewClicked() {
        if (isSwitch) {
            startActivity(new Intent(this, MainActivity.class));
            finish();
        } else {
            finish();
        }

    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK)) {
            if (isSwitch) {
                startActivity(new Intent(this, MainActivity.class));
                finish();
            } else {
                finish();
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
